package com.example.designsoft.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestOptions;
import com.example.designsoft.R;

import java.io.ByteArrayOutputStream;


/**
 * author : Gyk
 * time   : 2020/12/1
 * todo   :
 */
public class GlideUtils {

    public static void glideRound(Context context, String url, int radius, ImageView imageView) {
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = new RequestOptions()
                .transform(new GlideRoundTransform(context, radius))
                .override(300, 300);
        Glide.with(context)
                .load(url)
                .placeholder(R.mipmap.headdefault)
                .apply(options).
                into(imageView);
    }

    public static void glideUriRound(Context context, String url, int radius, ImageView imageView) {
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        RequestOptions options = new RequestOptions()
                .transform(new GlideRoundTransform(context, radius))
                .override(300, 300);
        Glide.with(context)
                .load(Uri.parse(url))
                .placeholder(R.mipmap.headdefault)
                .apply(options)
                .into(imageView);
    }

    public static void glideCenterCrop(Context context, String url, int radius, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), new GlideRoundTransform(context, radius));
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(imageView);
    }

    public static void glideBitmap(Context context, Bitmap mBitmap,int radius,ImageView imageView){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Bitmap bitmap = mBitmap;
        bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos);
        byte[] bytes=baos.toByteArray();
        RequestOptions options = new RequestOptions()
                .transform(new CenterCrop(), new GlideRoundTransform(context, radius));
        Glide.with(context)
                .load(bytes)
                .apply(options)
                .into(imageView);
    }

}
